package jbl.action;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jbl.io.excel.ExportExcel;
import jbl.io.excel.XgExcel;
import jbl.pageModel.Xg;
import jbl.service.XgServiceI;
import jbl.util.Json;
import jbl.util.Message;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
/**
 * 
 * @author jblcxg
 * 
 * 箱柜信息管理  
 *
 */
@Namespace("/")
@Action(value="xgAction")
public class XgAction extends BaseAction implements ModelDriven<Xg>{
	private Xg xg=new Xg();
	
	private XgServiceI xgService;
	
	@Autowired
	public void setXgService(XgServiceI xgService) {
		this.xgService = xgService;
	}

	@Override
	public Xg getModel() {
		return xg;
	}
	/**
	 * 返回箱柜DataGrid
	 */
	public void dataGrid(){
		writeJson(xgService.dataGrid(xg));
	}
	/**
	 * 添加箱柜
	 */
	public void add(){
		writeJson(xgService.add(xg));
	}
	/**
	 * 修改箱柜
	 */
	public void edit(){
		writeJson(xgService.edit(xg));
	}
	/**
	 * 删除箱柜
	 */
	public void delete(){
		writeJson(xgService.delete(xg.getIds()));
	}
	
	/**
	 * 上传excel文件
	 */
	public void upXgFile(){
		writeJson(xgService.upXgFile(xg));
	}
	/**
	 * 下载excel文件
	 **/
	public void exportxgExcel(){
			Json j = new Json();
			HttpServletResponse response = ServletActionContext.getResponse();
			try {
				
				List<XgExcel> dataset = xgService.getXgExcelList(xg); 
				if(dataset != null &&dataset.size() > 0){
					String[] hearders = new String[] {Message.XH,Message.XG_BH, Message.XGMC, Message.HJBH, Message.HJMC,Message.KFMC,Message.ZT, Message.BZ, Message.CJSJ};//表头数组
	
					ExportExcel<XgExcel> ex = new ExportExcel<XgExcel>();
					
					String filename = Message.dateFormat.format(new Date())+Message.XG_EXCEL_NAME+".xls";
					response.setContentType("application/ms-excel;charset=UTF-8");
					try {
						//response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
						response.setHeader("Content-disposition","attachment; filename="+    new String(filename.getBytes("gb2312"), "ISO8859-1"));//filename是下载的xls的名，建议最好用英文
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
						j.setMsg("导出失败， 编码错误!");
						writeJson(j);
					}
					OutputStream out = response.getOutputStream();
			        ex.exportExcel(Message.XG_EXCEL_NAME,hearders, dataset, out,Message.FORMAT_TIME);
			        out.close();
				}else{
					j.setMsg("导出失败， 数据为空!");
					writeJson(j);
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				j.setMsg("导出失败!");
				writeJson(j);
			}

	}
	/**
	 * 下拉列表值
	 */
	public void comboboxInfo(){
		writeJson(xgService.comboboxInfo());
	}
}
